package com.beetlestech.nfe.main.biz.com.impl;

import java.sql.Time;
import java.util.Date;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbPaymentOrder;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.StudentorderDTO;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;

public class HbPaymentOrderImpl extends AbstractDBLogic implements HbPaymentOrder, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	@Override
	public void paymentOrder(
			String actionType,
			String orderSql,
			Integer orderId,
			Integer paymenttype) {
		if(actionType.equals("single")){
			StudentorderDTO  studentorderDTO=new StudentorderDTO();
			studentorderDTO.setorderid(orderId);
			studentorderDTO.setstate(Integer.parseInt(CATAGORY_P_ORDER_STATE.PAID.toString()));
			//洪雷不显示付款日期 注释
			Date date=new Date();
			Time send_time=new Time(date.getTime());
			java.sql.Date sendSql= new java.sql.Date(date.getTime());
			studentorderDTO.setpaymentdate(sendSql);
			studentorderDTO.setpaymenttime(send_time);
			studentorderDTO.setpaymenttype(paymenttype);
			updateDAO.updateOne(StudentorderDTO.UPDATE, studentorderDTO, M10190WS);
			OrderdetailDTO orderdetailDTO =new OrderdetailDTO();
			orderdetailDTO.setorderid(orderId);
			orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.NOT_OPENED.toString()));
			updateDAO.update(SQLID.ORDER_DETAIL_UPDATE_BY_ORDER_ID, orderdetailDTO);
			//通过order id 查询studentid
			StudentorderDTO  studentDTO=new StudentorderDTO();
			studentDTO.setorderid(orderId);
			StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
			createMessage(orderId,sDTO.getstudentid());//给学生发送一条消息
		
		}else if(actionType.equals("many")){
			String[] dbSqlArray=orderSql.split(",");
			for (int i = 0; i < dbSqlArray.length; i++) {
				StudentorderDTO  studentorderDTO=new StudentorderDTO();
				studentorderDTO.setorderid(Integer.parseInt(dbSqlArray[i]));
				studentorderDTO.setstate(Integer.parseInt(CATAGORY_P_ORDER_STATE.PAID.toString()));
				studentorderDTO.setpaymenttype(paymenttype);
				updateDAO.updateOne(StudentorderDTO.UPDATE, studentorderDTO, M10190WS);
				OrderdetailDTO orderdetailDTO =new OrderdetailDTO();
				orderdetailDTO.setorderid(Integer.parseInt(dbSqlArray[i]));
				orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.NOT_OPENED.toString()));
				updateDAO.update(SQLID.ORDER_DETAIL_UPDATE_BY_ORDER_ID, orderdetailDTO);
			}
		}
	}
	
	private void createMessage(Integer orderID, Integer studentID){
		// 创建消息
		UsermessageDTO userMessage = new UsermessageDTO();
		userMessage.setTitel("您的订单【" + orderID + "】已经确认支付成功!");
		userMessage.setMessage("您的订单【" + orderID + "】已经确认支付成功!");
		userMessage.setSenderId(SERVER_ID);
		userMessage.setReceiveId(studentID);
		userMessage.setSendDT(getCurrentDate());
		userMessage.setSendTime(getCurrentTime());
		userMessage.setReadflg(Integer.parseInt(CATAGORY_P_MESSAGE_STATE.UNREAD.toString()));
		updateDAO.update(UsermessageDTO.INSERT, userMessage);
	}

}
